package com.mine.site.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mine.site.model.ShoppingCart;
import com.mine.site.util.DbUtil;

public class SuccessDao {
	public void findAll() {
		String sql="INSERT INTO success SELECT * FROM shoppingcart";
		PreparedStatement pst = null;
		try {
			pst = DbUtil.getInstance().getConn().prepareStatement(sql);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public List<ShoppingCart> findAll1() {
		List<ShoppingCart> cart1 = new ArrayList<ShoppingCart>();
		String sql = "select * from success";
		Statement stmt = null;
		try {
			stmt = DbUtil.getInstance().getConn().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				ShoppingCart u = new ShoppingCart();
				u.setId(rs.getString(1));
				u.setName(rs.getString(2));
				u.setNumber(rs.getString(3));
				u.setTitle(rs.getString(4));
				u.setPrice(rs.getString(5));
				u.setUrl(rs.getString(6));
				cart1.add(u);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return cart1;
	}
	
	public List<ShoppingCart> findByName(String Id) {
		List<ShoppingCart> commo = new ArrayList<ShoppingCart>();
		String sql = "select * from success where id = ? ";
		PreparedStatement stmt = null;
		try {
			stmt = DbUtil.getInstance().getConn().prepareStatement(sql);
			stmt.setString(1, Id );
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				ShoppingCart u = new ShoppingCart();
				u.setId(rs.getString(1));
				u.setName(rs.getString(2));
				u.setNumber(rs.getString(3));
				u.setTitle(rs.getString(4));
				u.setPrice(rs.getString(5));
				u.setUrl(rs.getString(6));
				commo.add(u);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return commo;
	}
	public void delete(String Url){
		String sql = "delete from success where url=?";
		PreparedStatement pst = null;
		try {
			pst = DbUtil.getInstance().getConn().prepareStatement(sql);
			pst.setString(1, Url);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
